package com.zzf.concurrent.example.single;

/**
 * 懒汉式单例-双重检查，需要通过volatile关键字来防止指令重排序
 * @author GaoFeng2017
 * @date 2019/8/12 19:13
 */
public class DoubleCheckSingleBean {

    private volatile static DoubleCheckSingleBean doubleCheckSingleBean;

    private DoubleCheckSingleBean() {

    }

    public static DoubleCheckSingleBean getDoubleCheckSingleBean() {

        if (doubleCheckSingleBean == null) {
            synchronized (DoubleCheckSingleBean.class) {
                if (doubleCheckSingleBean == null) {
                    doubleCheckSingleBean = new DoubleCheckSingleBean();
                }
            }
        }

        return doubleCheckSingleBean;
    }
}
